package dca;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class SymptomRegistry {
	
	private ArrayList<Symptom> SymptomList = new ArrayList<Symptom>();
	private String filename;
	
	public SymptomRegistry(){};
	public SymptomRegistry(String filename) throws IOException{
		this.filename = filename;
		loadSymptoms(this.filename);
	}
	
	
	/**
	 * Reads a text file of symptoms and loads the symptoms into the registry.
	 * @return
	 */
	public void loadSymptoms(String filename) throws IOException{
		File inFile = new File("data/"+filename+".txt");
		Scanner in = new Scanner(inFile);
		try
		{
		readData(in);
		}
		finally
		{
		in.close();			
		}
		
		//Scanner in = new Scanner(new File("/data/"+filename+".txt"));		
	}
	
	private void readData(Scanner in) throws BadDataException{
		while(in.hasNextLine()){
			String nameR = in.nextLine();
			Pattern P = Pattern.compile("[,\\n]+");
			String[] result = P.split(nameR);
			SymptomList.add(new Symptom(result[0],result[1]));
		}
	}
	
	/**
	 * Adds a new symptom to the registry if it does not already exists.
	 * @param Name
	 * @param level
	 */
	public void addSymptom(String name, Severity level){
		for(Symptom s:SymptomList)
		{
			if(s.getName().equalsIgnoreCase(name))
				return;
		}
		SymptomList.add(new Symptom(name,level));
	}
	public void addSymptom(Symptom symptom){
		this.addSymptom(symptom.getName(), symptom.getSeverity());
	}
	
	/**
	 * Saves the current symptoms as a text file.
	 * @throws FileNotFoundException 
	 */
	public void saveSymptoms(String filename) throws FileNotFoundException{
		PrintWriter out = new PrintWriter("data/"+filename+".txt");
		for(Symptom s:SymptomList)
		{
			out.println(s.toString());
		}
		out.close();
	}
	public void saveSymptoms() throws FileNotFoundException{
		this.saveSymptoms(this.filename);
	}
	
	/**
	 * Finds a symptom by name.
	 * @param Name
	 * @return
	 */
	public Symptom getSymptom(String Name){
		for(Symptom s:SymptomList)
			if (s.getName().equalsIgnoreCase(Name))
				return s;
		throw new NoSuchElementException("Symptom "+Name+". Does not exsist in symptom registry.");
	}
	
	
	/**
	 * Returns a list of all symptoms in the order set by setOrder(). 
	 */
	public ArrayList<Symptom> SymptomList(){
		Collections.sort(SymptomList);
		return SymptomList;
	}
	
	/**
	 * Chooses wheater SymptomList() returns the list in alphabetical order or by order of the severity of the symptom.
	 * @param order can be "Alphabet" or "Severity"
	 * @throws BadDataException
	 */
	public void setOrder(String order) throws BadDataException{
		for (Symptom s: SymptomList)
		{
			s.sortBy(order);
		}
	}
	
	//Useful for adding symptoms to a database or checking where you are working.
	public String getFilename(){
		return this.filename;
	}

	
}
